import 'package:atcflutter/home/main_page.dart';
import 'package:atcflutter/user/model/user_model.dart';
import 'package:atcflutter/widget/root_widget.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';

/// 登录页面
class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final accountController = TextEditingController();
    final pwController = TextEditingController();

    return RootWidget(builder: (context, size) {
      return Stack(
        alignment: Alignment.bottomCenter,
        children: <Widget>[
          Image.asset(
            "image/bg_login.png",
            fit: BoxFit.cover,
            width: size.width,
            height: size.height,
          ),
          Container(
            margin: EdgeInsets.only(top: 120),
            width: size.width,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  "image/logo_login.png",
                  width: 150,
                  height: 70,
                  fit: BoxFit.contain,
                ),
                _buildAccountInputWidget(accountController),
                _buildPwInputWidget(pwController),
                GestureDetector(
                    onTap: () {
                      _onLogin(context, accountController, pwController);
                    },
                    child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.only(top: 12, bottom: 12),
                        margin: EdgeInsets.only(left: 30, right: 30, top: 15),
                        width: size.width,
                        color: Colors.deepOrangeAccent,
                        child:
                            Text("登录", style: TextStyle(color: Colors.white))))
              ],
            ),
          ),
          Container(
              margin: EdgeInsets.only(bottom: 30),
              child: Image.asset("image/logo_usedcar.png", width: 150))
        ],
      );
    });
  }
}

///登录操作
_onLogin(BuildContext context, TextEditingController accountController,
    TextEditingController pwController) {
  String account = accountController.text;
  String pwd = pwController.text;
  if (account.isEmpty || pwd.isEmpty) {
    BotToast.showText(text: "账号和密码不能为空");
  } else {
    UserModel.login(account, pwd).then((result) {
      //登录成功
      UserModel.setUserInfo(result["result"]);
      BotToast.showText(
          text: "登录成功",
          onClose: () {
            Future.delayed(Duration(milliseconds: 10), () {
              Navigator.pushAndRemoveUntil(context,
                  MaterialPageRoute(builder: (_) => MainPage()),
                      (route) => false);
            });
          });
    }).catchError((error) {
      print(error.message);
      BotToast.showText(text: error.message);
    });
  }
}

///密码
Widget _buildPwInputWidget(TextEditingController pwController) {
  return Container(
    margin: EdgeInsets.only(top: 10),
    padding: EdgeInsets.only(left: 30, right: 30),
    child: TextFormField(
      controller: pwController,
      style: TextStyle(fontSize: 15, color: Colors.white),
      maxLines: 1,
      obscureText: true,
      decoration: const InputDecoration(
          icon: Text(
            "密码",
            style: TextStyle(color: Colors.white),
          ),
          hintText: "请输入您的密码",
          hintStyle: TextStyle(color: Colors.white),
          labelStyle: TextStyle(color: Colors.white),
          border:
              UnderlineInputBorder(borderSide: BorderSide(color: Colors.white)),
          focusedBorder:
              UnderlineInputBorder(borderSide: BorderSide(color: Colors.white)),
          fillColor: Colors.white,
          focusColor: Colors.white,
          hoverColor: Colors.white),
    ),
  );
}

///账号
Widget _buildAccountInputWidget(TextEditingController accountController) {
  return Container(
    margin: EdgeInsets.only(top: 50),
    padding: EdgeInsets.only(left: 30, right: 30),
    child: TextFormField(
      controller: accountController,
      style: TextStyle(fontSize: 15, color: Colors.white),
      maxLines: 1,
      decoration: const InputDecoration(
          icon: Text(
            "账号",
            style: TextStyle(color: Colors.white),
          ),
          hintText: "请输入您的账号",
          hintStyle: TextStyle(color: Colors.white),
          labelStyle: TextStyle(color: Colors.white),
          border:
              UnderlineInputBorder(borderSide: BorderSide(color: Colors.white)),
          focusedBorder:
              UnderlineInputBorder(borderSide: BorderSide(color: Colors.white)),
          fillColor: Colors.white,
          focusColor: Colors.white,
          hoverColor: Colors.white),
    ),
  );
}
